<?php

/**
 * 巡查事件控制器
 * User: Administrator
 * Date: 2017/10/17 0017
 * Time: 11:44
 */

namespace Admin\Controller;

class ObjectController extends CommonController
{

    /**
     * 项目类型列表
     */
    public function type_list()
    {
        $list = $this->lists('ObjectType', 'status = 1', 'id asc');
        $this->assign('list', $list);

        $Data = M('ObjectType'); // 实例化Data数据模型
        $count   = $Data->where( 'status = 1')->count();
        $p = getpage($count,20);
        $this->assign('page', $p->show()); // 赋值分页输出

        $this->display('type_list');
    }

    /**
     * 添加项目类型
     */
    public function add_type()
    {
        if (IS_POST) {
            $name = I('post.name');
            $data = [
                'name' => $name,
                'status' => 1,
            ];
            $res = M('ObjectType')->add($data);
            
            $this->success('添加成功！', U('Object/type_list'));
        }
        $this->display('add_type');
    }

    /**
     * 编辑项目类型
     */
    public function edit_type()
    {
        if (IS_POST) {
            $id = I('post.id');
            $name = I('post.name');
            $data = [
                'name' => $name,
            ];
            $res = M('ObjectType')->where(['id' => $id])->save($data);
            $this->success('保存成功。', U('Object/type_list'));
        }
        $id = I('id');
        $info = M('ObjectType')->where(['id' => $id])->find();
        $this->assign('info', $info);
        $this->display('edit_type');
    }

    /**
     * 删除项目类型
     */
    public function del_type()
    {
        $id = I('id');
        M('ObjectType')->where(['id' => $id])->setField('status', 0);
        $this->success('删除成功', U('Object/type_list'));
    }

    /**
     * 风险点列表
     */
    public function danger_list()
    {   
        $list = $this->lists('danger', 'status = 1', 'id desc');
        if (!empty($list)) {
            foreach ($list as $k=>$v) {
                $list[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
            }
        }
        $this->assign('list', $list);

        $Data = M('danger'); // 实例化Data数据模型
        $count   = $Data->where( 'status = 1')->count();
        $p = getpage($count,20);
        $this->assign('page', $p->show()); // 赋值分页输出

        $this->display('danger_list');
    }

    /**
     * 添加风险点
     */
    public function add_danger()
    {
        if (IS_POST) {
            $name = I('post.name');
            $type_id = I('post.type_id');
            $data = [
                'name' => $name,
                'type_id' => $type_id,
                'status' => 1,
            ];
            $res = M('Danger')->add($data);
            
            $this->success('添加成功！', U('Object/danger_list'));
        }
        $type = M('ObjectType')->where(['status' => 1])->select();
        $this->assign('type', $type);
        $this->display('add_danger');
    }

    /**
     * 编辑风险点
     */
    public function edit_danger()
    {
        if (IS_POST) {
            $id = I('post.id');
            $name = I('post.name');
            $type_id = I('post.type_id');
            $data = [
                'name' => $name,
                'type_id' => $type_id,
            ];
            $res = M('Danger')->where(['id' => $id])->save($data);
            $this->success('保存成功。', U('Object/danger_list'));
        }
        $id = I('id');
        $info = M('Danger')->where(['id' => $id])->find();
        $type = M('ObjectType')->where(['status' => 1])->select();
        $this->assign('type', $type);
        $this->assign('info', $info);
        $this->display('edit_danger');
    }


    /**
     * 删除风险点
     */
    public function del_danger()
    {
        if(IS_AJAX){
            $id = I('id');
            M('Danger')->where(['id' => $id])->delete();
            $this->success('删除成功', U('Object/danger_list'));
        }
        
    }

    /**
     * 巡查事物列表
     */
    public function object_list()
    {
        $list = $this->lists('Object', 'status = 1', 'object_id asc');
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
                $list[$k]['danger_name'] = M('Danger')->where(['id' => $v['danger_id']])->getField('name');
                $list[$k]['object_level_name'] = M('ObjectLevel')->where(['id' => $v['object_level_id']])->getField('name');
                $list[$k]['workshop'] = M('Workshop')->where(['id' => $v['workshop_id']])->getField('name');
                $list[$k]['danger_level_name'] = M('danger_level')->where(['id'=>$v['danger_level_id']])->getField('name');
            }
        }
        $this->assign('list', $list);

        $Data = M('Object'); // 实例化Data数据模型
        $count   = $Data->where( 'status = 1')->count();
        $p = getpage($count,20);
        $this->assign('page', $p->show()); // 赋值分页输出

        $this->display('object_list');
    }

    /**
     * 增加巡查事项
     */
    public function add_object()
    {
        if (IS_POST) {
            $post = I('post.');
            $post['create_time'] = date('Y-m-d H:i:s', time());
            $res = M('Object')->add($post);
            $this->success('添加成功！', U('Object/object_list'));
        }
        $where['status'] = 1;
        $type = M('ObjectType')->where($where)->select();
        $danger = M('Danger')->where($where)->select();
        $danger_level = M('DangerLevel')->where($where)->select();
        $object_level = M('ObjectLevel')->where($where)->select();
        $workshop = M('Workshop')->where($where)->select();
        $this->assign('type', $type);
        $this->assign('danger', $danger);
        $this->assign('danger_level', $danger_level);
        $this->assign('object_level', $object_level);
        $this->assign('workshop', $workshop);
        $this->display('add_object');
    }
	
	public function add_csv()
	{
		if (IS_POST) {
			if (isset($_FILES['object'])) {
				$file = fopen($_FILES["object"]["tmp_name"], 'r');
				$data = input_csv($file);
				for($i = 1; $i < count($data); $i++) {   
					$data[$i][0] = iconv('gb2312', 'utf-8', $data[$i][0]); //中文转码   
					$data[$i][1] = iconv('gb2312', 'utf-8', $data[$i][1]);
					$data[$i][2] = iconv('gb2312', 'utf-8', $data[$i][2]); 
					$data[$i][4] = iconv('gb2312', 'utf-8', $data[$i][4]); 
					$data[$i][5] = iconv('gb2312', 'utf-8', $data[$i][5]); 
					$data[$i][6] = iconv('gb2312', 'utf-8', $data[$i][6]); 
					$data[$i][7] = iconv('gb2312', 'utf-8', $data[$i][7]);
					$data[$i][8] = iconv('gb2312', 'utf-8', $data[$i][8]);
					$data[$i][9] = iconv('gb2312', 'utf-8', $data[$i][9]);
					$data[$i][10] = iconv('gb2312', 'utf-8', $data[$i][10]);
					$data[$i][11] = iconv('gb2312', 'utf-8', $data[$i][11]);
					$data[$i][12] = iconv('gb2312', 'utf-8', $data[$i][12]);
				}
				for($i = 1; $i < count($data); $i++) {   
					$data[$i][0] = M('ObjectType')->where(['name'=>$data[$i][0]])->getField('id'); 
					$data[$i][1] = M('Danger')->where(['name'=>$data[$i][1]])->getField('id');
					$data[$i][11] = M('ObjectLevel')->where(['name'=>$data[$i][11]])->getField('id'); 
				}
				for($i = 1; $i < count($data); $i++) {   
					$tmp = [
						'type_id' => $data[$i][0],
						'danger_id' => $data[$i][1],
						'name' => $data[$i][2],
						'danger_level_id' => $data[$i][3],
						'danger_source' => $data[$i][4],
						'accident_possible' => $data[$i][5],
						'engineering_method' => $data[$i][6],
						'manager_method' => $data[$i][7],
						'training_method' => $data[$i][8],
						'personal_method' => $data[$i][9],
						'continrcy_method' => $data[$i][10],
						'object_level_id' => $data[$i][11],
						'remark' => $data[$i][12],
						'status' => 1,
						'create_time' => time(),
					];
					M('Object')->add($tmp);
				}
				//$this->success('success', U('Object/object_list'));
                $this->success('success');
			}
		}
		$this->display('add_csv');
	}

    /**
     * 编辑巡查事项
     */
    public function edit_object()
    {
        if (IS_POST) {
            $post = I('post.');
            $res = M('Object')->where(['object_id' => $post['object_id']])->save($post);
            $this->success('修改成功！', U('Object/object_list'));
        }
        $object_id = I('object_id');
        $info = M('Object')->where(['object_id'=>$object_id])->find();
        $this->assign('info', $info);
        $where['status'] = 1;
        $type = M('ObjectType')->where($where)->select();
        $danger = M('Danger')->where($where)->select();
        $danger_level = M('DangerLevel')->where($where)->select();
        $object_level = M('ObjectLevel')->where($where)->select();
        $workshop = M('Workshop')->where($where)->select();
        $this->assign('type', $type);
        $this->assign('danger', $danger);
        $this->assign('danger_level', $danger_level);
        $this->assign('object_level', $object_level);
        $this->assign('workshop', $workshop);
        $this->display('edit_object');
    }

    /**
     * 删除巡查事项
     */
    public function del_object()
    {
        if(IS_AJAX){
            $object_id = I('object_id');
            $res = M('Object')->where(['object_id' => $object_id])->setField('status', 0);
            $this->success('删除成功！', U('Object/object_list'));
        }
        
    }

    /**
     * 项目级别列表
     */
    public function ob_level_list()
    {
        $list = $this->lists('ObjectLevel', 'status = 1', 'id asc');
        $this->assign('list', $list);
        $Data = M('object_level'); // 实例化Data数据模型
        $count   = $Data->where( 'status = 1')->count();
        $p = getpage($count,20);
        $this->assign('page', $p->show());
        $this->display('ob_level_list');
    }

    /**
     * 添加项目级别
     */
    public function add_ob_level()
    {
        if (IS_POST) {
            $name = I('post.name');
            $data = [
                'name' => $name,
                'status' => 1,
            ];
            $res = M('ObjectLevel')->add($data);
            $this->success('添加成功！', U('Object/ob_level_list'));
        }
        $this->display('add_ob_level');
    }

    /**
     * 编辑项目级别
     */
    public function edit_ob_level()
    {
        if (IS_POST) {
            $id = I('post.id');
            $name = I('post.name');
            $data = [
                'name' => $name,
            ];
            $res = M('ObjectLevel')->where(['id' => $id])->save($data);
            $this->success('保存成功。', U('Object/ob_level_list'));
        }
        $id = I('id');
        $info = M('ObjectLevel')->where(['id' => $id])->find();
        $this->assign('info', $info);
        $this->display('edit_ob_level');
    }

    /**
     * 删除项目级别
     */
    public function del_ob_level()
    {
        if(IS_AJAX){
            $id = I('id');
            M('ObjectLevel')->where(['id' => $id])->delete();
            $this->success('删除成功', U('Object/ob_level_list')); 
        }
        
    }

    /**
     * 风险级别列表
     */
    public function danger_level_list()
    {
        $list = $this->lists('DangerLevel', 'status = 1', 'id asc');
        $this->assign('list', $list);
        $Data = M('danger_level'); // 实例化Data数据模型
        $count   = $Data->where( 'status = 1')->count();
        $p = getpage($count,20);
        $this->assign('page', $p->show());
        $this->display('danger_level_list');
    }

    /**
     * 添加风险级别
     */
    public function add_danger_level()
    {
        if (IS_POST) {
            $name = I('post.name');
            $data = [
                'name' => $name,
                'status' => 1,
            ];
            $res = M('DangerLevel')->add($data);
            $this->success('添加成功！', U('Object/danger_level_list'));
        }
        $this->display('add_danger_level');
    }

    /**
     * 编辑风险级别
     */
    public function edit_danger_level()
    {
        if (IS_POST) {
            $id = I('post.id');
            $name = I('post.name');
            $data = [
                'name' => $name,
            ];
            $res = M('DangerLevel')->where(['id' => $id])->save($data);
            $this->success('保存成功。', U('Object/danger_level_list'));
        }
        $id = I('id');
        $info = M('DangerLevel')->where(['id' => $id])->find();
        $this->assign('info', $info);
        $this->display('edit_danger_level');
    }

    /**
     * 删除风险级别
     */
    public function del_danger_level()
    {
        if(IS_AJAX){
          $id = I('id');
          M('DangerLevel')->where(['id' => $id])->delete();
          $this->success('删除成功', U('Object/danger_level_list'));  
        }
        
    }
}
